package com.fau.wm.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.hibernate.annotations.GenericGenerator;
import org.json.simple.JSONAware;
import org.json.simple.JSONObject;

import com.fau.wm.util.SystemConfig;

/**
 * 库存调拨表
 * @author liy
 * @since 2014年7月24日,下午5:10:38
 */
@Entity
@Table(name = "medical_allocation")
public class MedicalAllocation implements Serializable, JSONAware{

	private static final long serialVersionUID = -3154876796455176723L;
	
	private String id;
	private String serialNum;					// 编号
	private String title;						// 标题
	private String writeUserId;					// 填写人id
	private String auditUserId;					// 审核人id
	private String recallWhId;					// 调出仓库id
	private String callinWhId;					// 调入仓库id
	private Date allocationTime;				// 调拨时间
	private String status = "3";				// 调拨状态 0 待审核，1调拨完成，2审核未通过， 3新建
	private Date createTime = new Date();		// 创建时间
	private Date updateTime = new Date();		// 更新时间
	
	private MedicalAdmin writeUser;				// 填单人
	private MedicalAdmin auditUser;				// 审核人
	private MedicalWarehouse recallWarehouse;	// 调出仓库实体类
	private String recallWarehouseName;         // 调出仓库名称
	private MedicalWarehouse callinWarehouse;	// 调入仓库实体类
	private String callinWarehouseName;         // 调入仓库名称
	private String hospitalId;					//医院标识id
	
	@Id
	@GenericGenerator(name = "generator", strategy = "uuid.hex")
	@GeneratedValue(generator = "generator")
	@Column(name = "id", length = 32)
	public String getId() {
		return id;
	}
	
	public void setId(String id) {
		this.id = id;
	}
	
	@Column(name = "serial_num", length = 50)
	public String getSerialNum() {
		return serialNum;
	}

	public void setSerialNum(String serialNum) {
		this.serialNum = serialNum;
	}

	@Column(name = "title", length = 255)
	public String getTitle() {
		return title;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}

	@Transient
	public String getWriteUserId() {
		return writeUserId;
	}

	public void setWriteUserId(String writeUserId) {
		this.writeUserId = writeUserId;
	}
	
	@Transient
	public String getAuditUserId() {
		return auditUserId;
	}

	public void setAuditUserId(String auditUserId) {
		this.auditUserId = auditUserId;
	}

	@Transient
	public String getRecallWhId() {
		return recallWhId;
	}

	public void setRecallWhId(String recallWhId) {
		this.recallWhId = recallWhId;
	}

	@Transient
	public String getRecallWarehouseName() {
		return recallWarehouseName;
	}

	public void setRecallWarehouseName(String recallWarehouseName) {
		this.recallWarehouseName = recallWarehouseName;
	}
	
	@Transient
	public String getCallinWarehouseName() {
		return callinWarehouseName;
	}

	public void setCallinWarehouseName(String callinWarehouseName) {
		this.callinWarehouseName = callinWarehouseName;
	}

	@Transient
	public String getCallinWhId() {
		return callinWhId;
	}

	public void setCallinWhId(String callinWhId) {
		this.callinWhId = callinWhId;
	}
	
	@Column(name = "allocation_time")
	public Date getAllocationTime() {
		return allocationTime;
	}

	public void setAllocationTime(Date allocationTime) {
		this.allocationTime = allocationTime;
	}
	
	@Column(name = "status", length = 5)
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	@Column(name = "create_time")
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "update_time")
	public Date getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(Date updateTime) {
		this.updateTime = updateTime;
	}
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "write_user_id")	
	public MedicalAdmin getWriteUser() {
		return writeUser;
	}

	public void setWriteUser(MedicalAdmin writeUser) {
		this.writeUser = writeUser;
	}
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "audit_user_id")
	public MedicalAdmin getAuditUser() {
		return auditUser;
	}

	public void setAuditUser(MedicalAdmin auditUser) {
		this.auditUser = auditUser;
	}

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "recall_wh_id")
	public MedicalWarehouse getRecallWarehouse() {
		return recallWarehouse;
	}

	public void setRecallWarehouse(MedicalWarehouse recallWarehouse) {
		this.recallWarehouse = recallWarehouse;
	}
	
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "callin_wh_id")
	public MedicalWarehouse getCallinWarehouse() {
		return callinWarehouse;
	}

	public void setCallinWarehouse(MedicalWarehouse callinWarehouse) {
		this.callinWarehouse = callinWarehouse;
	}
	
	@Column(name="hospital_id",length = 32)
	public String getHospitalId() {
		return hospitalId;
	}

	public void setHospitalId(String hospitalId) {
		this.hospitalId = hospitalId;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String toJSONString() {
		JSONObject obj = new JSONObject();
		obj.put("id", id);
		obj.put("serialNum", serialNum);
		obj.put("title", title);
		obj.put("recallWhId", recallWarehouse.getId());
		obj.put("recallWhName", recallWarehouse.getName());
		obj.put("callinWhId", callinWarehouse.getId());
		obj.put("callinWhName", callinWarehouse.getName());
		obj.put("writeUserId", writeUser.getId());
		obj.put("writeUserName", writeUser.getName());
		if(null != auditUser){
			obj.put("auditUserName", auditUser.getName());
		}
		obj.put("allocationTime", DateFormatUtils.format(allocationTime, "yyyy-MM-dd"));
		obj.put("statusVal", status);
		if(SystemConfig.ALLOCATION_STATUS_PENDING.equals(status)) {
			obj.put("status", "待审核");
		}else if(SystemConfig.ALLOCATION_STATUS_APPROVAL.equals(status)) {
			obj.put("status", "调拨完成");
		}else if(SystemConfig.ALLOCATION_STATUS_REJECT.equals(status)) {
			obj.put("status", "审核未通过");
		}else if(SystemConfig.ALLOCATION_STATUS_NEW.equals(status)) {
			obj.put("status", "新建");
		}
		
		return obj.toString();
	}
}